package com.study.udf;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

public class MyUdf extends GenericUDF {
    @Override
    public ObjectInspector initialize(ObjectInspector[] objectInspectors) throws UDFArgumentException {
        //1.判断参数是否唯一
        if (objectInspectors.length != 1)
            //不为一抛出异常
            throw new UDFArgumentException("参数不为一");
        //返回输出参数类型
        return PrimitiveObjectInspectorFactory.javaIntObjectInspector;
    }

    //执行过程
    @Override
    public Object evaluate(DeferredObject[] deferredObjects) throws HiveException {
        //1.非空判断
        DeferredObject deferredObject = deferredObjects[0];
        if (deferredObject == null)
            return 0;
        //2.返回字符串长度
        return deferredObject.get().toString().length();
    }

    @Override
    public String getDisplayString(String[] strings) {
        return null;
    }
}
